<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>交换数组中的键和值</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-flip" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_flip</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_flip</span> &mdash; <span class="dc-title">交换数组中的键和值</span></p>

   </div>
   
 <div class="refsect1 description" id="refsect1-function.array-flip-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_flip</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
   ) : <span class="type">array</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>array_flip()</strong></span> 返回一个反转后的 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，例如
     <code class="parameter">array</code> 中的键名变成了值，而
     <code class="parameter">array</code> 中的值成了键名。
    </p>
    <p class="para">
     注意 <code class="parameter">array</code> 中的值需要能够作为合法的键名（例如需要是
     <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 或者 <span class="type"><a href="language.types.string.html" class="type string">string</a></span>）。如果类型不对，将出现一个警告，并且有问题的键／值对<em class="emphasis">将不会出现在结果里</em>。
    </p>
    <p class="para">
     如果同一个值出现多次，则最后一个键名将作为它的值，其它键会被丢弃。
    </p>
   </div>

   
   
 <div class="refsect1 parameters" id="refsect1-function.array-flip-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       要交换键/值对的数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-flip-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回交换后的数组，如果失败返回 <strong><code>NULL</code></strong>。
  </p>
 </div>



 <div class="refsect1 examples" id="refsect1-function.array-flip-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6030">
    <p><strong>Example #1 <span class="function"><strong>array_flip()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"oranges"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"apples"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"pears"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$flipped&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_flip</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$flipped</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [oranges] =&gt; 0
    [apples] =&gt; 1
    [pears] =&gt; 2
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6031">
    <p><strong>Example #2 <span class="function"><strong>array_flip()</strong></span> 例子 : 冲突</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$flipped&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_flip</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$flipped</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [1] =&gt; b
    [2] =&gt; c
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-flip-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_values.html" class="function" rel="rdfs-seeAlso">array_values()</a> - 返回数组中所有的值</span></li>
    <li class="member"><span class="function"><a href="array_keys.html" class="function" rel="rdfs-seeAlso">array_keys()</a> - 返回数组中部分的或所有的键名</span></li>
    <li class="member"><span class="function"><a href="array_reverse.html" class="function" rel="rdfs-seeAlso">array_reverse()</a> - 返回单元顺序相反的数组</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>